﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace EncryptionServer.COM
{
    public class EnstryptMessage
    {
        public static void EncryptConfiguration()
        {
            // 使用什么类型的加密

            string provider = "RsaProtectedConfigurationProvider";

            Configuration config = null;

            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

            // 加密连接字符串

            ConfigurationSection section = config.ConnectionStrings;
            ConfigurationSectionGroup userSetSectionGroup = config.SectionGroups["userSettings"];
            ConfigurationSection userSetSection = userSetSectionGroup.Sections["EncryptionServer.Properties.Settings"];

            if ((section.SectionInformation.IsProtected == false) &&

                (section.ElementInformation.IsLocked == false))
            {
                section.SectionInformation.ProtectSection(provider);

                section.SectionInformation.ForceSave = true;

            }

            if (userSetSection.SectionInformation.IsProtected == false && userSetSection.ElementInformation.IsLocked == false)
            {
                userSetSection.SectionInformation.ProtectSection(provider);
                section.SectionInformation.ForceSave = true;

            }
            config.Save(ConfigurationSaveMode.Full);
        }
    }
}
